package com.guli.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.JwtUtils;
import com.guli.commonutils.R;
import com.guli.commonutils.ordervo.CourseWebVoOrder;
import com.guli.edu.client.OrdersClient;
import com.guli.edu.entity.Course;
import com.guli.edu.entity.chapter.ChapterVo;
import com.guli.edu.entity.frontVo.CourseFrontVo;
import com.guli.edu.entity.frontVo.CourseWebVo;
import com.guli.edu.service.ChapterService;
import com.guli.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CourseFrontController 
 * @Description: TODO
 * @author: liuzg
 * @Date: 2021/3/29 12:12
 * @Version: v1.0
 */
@RestController
@RequestMapping("/edu/coursefront")
@Api(tags = "前端课程功能")
public class CourseFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired  // 远程调用order服务
    private OrdersClient ordersClient;

    @ApiOperation(value = " 条件查询带分页查询课程")
    @PostMapping(value = "/getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<Course> pageCourse = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse, courseFrontVo);
        //返回分页所有数据
        return R.ok().data(map);
    }

    @ApiOperation(value = "课程详情的方法")
    @GetMapping(value = "/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //根据课程id和用户id查询当前课程是否已经支付过了
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = false;
        if (memberId == null){
            buyCourse = false;
        }else {
            buyCourse = ordersClient.isBuyCourse(courseId, memberId);
        }

        return R.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList).data("isBuy",buyCourse);
    }

    //根据课程id查询课程信息
    @ApiOperation(value = "根据课程id查询课程信息")
    @PostMapping(value = "/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo, courseWebVoOrder);
        return courseWebVoOrder;
    }


}
